<template>
  <div class="search-container">
    <div class="search-header">
      <h1>搜索音乐</h1>
      <div class="search-box">
        <input
          v-model="searchQuery"
          type="text"
          placeholder="搜索音乐、专辑或艺术家..."
          @keyup.enter="performSearch"
        />
        <button @click="performSearch" class="search-btn">
          <i class="el-icon-search"></i>
        </button>
      </div>
    </div>

    <!-- 搜索结果 -->
    <div v-if="searchResults.length > 0" class="search-results">
      <div class="result-section">
        <h2>搜索结果</h2>
        <div class="result-grid">
          <div
            v-for="item in searchResults"
            :key="item.id"
            class="result-item"
            @click="goToDetail(item)"
          >
            <img
              :src="item.coverUrl || 'https://via.placeholder.com/200x200/3498db/ffffff?text=音乐'"
              :alt="item.title"
            />
            <div class="item-info">
              <h3>{{ item.title }}</h3>
              <p>{{ item.creatorName || '未知艺术家' }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div v-else-if="hasSearched" class="empty-state">
      <div class="empty-icon">🔍</div>
      <h3>没有找到结果</h3>
      <p>试试其他关键词吧</p>
    </div>

    <!-- 热门推荐 -->
    <div v-else class="hot-recommendations">
      <h2>热门推荐</h2>
      <div class="recommendation-grid">
        <div
          v-for="item in recommendations"
          :key="item.id"
          class="recommendation-item"
          @click="goToDetail(item)"
        >
          <img
            :src="item.coverUrl || 'https://via.placeholder.com/200x200/e74c3c/ffffff?text=热门'"
            :alt="item.title"
          />
          <div class="item-info">
            <h3>{{ item.title }}</h3>
            <p>{{ item.creatorName || '未知艺术家' }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'SearchView',
  setup() {
    const router = useRouter()
    const searchQuery = ref('')
    const searchResults = ref([])
    const hasSearched = ref(false)

    // 模拟推荐数据
    const recommendations = ref([
      {
        id: 1,
        title: '夏日阳光',
        creatorName: '阳光乐队',
        coverUrl:
          'https://images.unsplash.com/photo-1493225457124-a3eb161ffa5f?w=200&h=200&fit=crop',
        type: 'music',
      },
      {
        id: 2,
        title: '海浪声声',
        creatorName: '海洋之声',
        coverUrl:
          'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=200&h=200&fit=crop',
        type: 'music',
      },
      {
        id: 3,
        title: '晚风温柔',
        creatorName: '温柔歌手',
        coverUrl:
          'https://images.unsplash.com/photo-1519904981063-b0cf448d479e?w=200&h=200&fit=crop',
        type: 'music',
      },
    ])

    const performSearch = () => {
      if (!searchQuery.value.trim()) return

      hasSearched.value = true
      // 模拟搜索结果
      if (searchQuery.value.toLowerCase().includes('夏日')) {
        searchResults.value = [
          {
            id: 1,
            title: '夏日阳光',
            creatorName: '阳光乐队',
            coverUrl:
              'https://images.unsplash.com/photo-1493225457124-a3eb161ffa5f?w=200&h=200&fit=crop',
            type: 'music',
          },
        ]
      } else if (searchQuery.value.toLowerCase().includes('海洋')) {
        searchResults.value = [
          {
            id: 2,
            title: '海浪声声',
            creatorName: '海洋之声',
            coverUrl:
              'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=200&h=200&fit=crop',
            type: 'music',
          },
        ]
      } else {
        searchResults.value = []
      }
    }

    const goToDetail = (item) => {
      if (item.type === 'music') {
        router.push(`/music/${item.id}`)
      } else if (item.type === 'album') {
        router.push(`/album/${item.id}`)
      }
    }

    return {
      searchQuery,
      searchResults,
      hasSearched,
      recommendations,
      performSearch,
      goToDetail,
    }
  },
}
</script>

<style scoped>
.search-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.search-header {
  text-align: center;
  margin-bottom: 40px;
}

.search-header h1 {
  font-size: 36px;
  color: #2c3e50;
  margin-bottom: 20px;
}

.search-box {
  display: flex;
  max-width: 600px;
  margin: 0 auto;
  gap: 10px;
}

.search-box input {
  flex: 1;
  padding: 15px 20px;
  border: 2px solid #e0e0e0;
  border-radius: 30px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s ease;
}

.search-box input:focus {
  border-color: #3498db;
}

.search-btn {
  padding: 15px 30px;
  background: linear-gradient(135deg, #3498db, #2980b9);
  color: white;
  border: none;
  border-radius: 30px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.search-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
}

.search-results,
.hot-recommendations {
  margin-top: 40px;
}

.result-section h2,
.hot-recommendations h2 {
  font-size: 24px;
  color: #2c3e50;
  margin-bottom: 20px;
}

.result-grid,
.recommendation-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}

.result-item,
.recommendation-item {
  background: white;
  border-radius: 12px;
  padding: 15px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s ease;
}

.result-item:hover,
.recommendation-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.result-item img,
.recommendation-item img {
  width: 100%;
  height: 150px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 10px;
}

.item-info h3 {
  font-size: 16px;
  color: #2c3e50;
  margin: 0 0 5px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-info p {
  font-size: 14px;
  color: #7f8c8d;
  margin: 0;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 20px;
}

.empty-state h3 {
  font-size: 20px;
  margin: 0 0 10px 0;
  color: #666;
}

.empty-state p {
  font-size: 16px;
  margin: 0;
}

@media (max-width: 768px) {
  .search-box {
    flex-direction: column;
  }

  .result-grid,
  .recommendation-grid {
    grid-template-columns: 1fr;
  }
}
</style>
